package com.greenline.push.services;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.PriorityBlockingQueue;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import com.greenline.push.service.core.ProcessQueueList;
import com.greenline.push.service.core.counter.CounterManager;
import com.greenline.push.service.core.item.Packet;
import com.greenline.push.service.core.task.SendStatisticsDataSchedule;
import com.greenline.push.service.core.utils.Constants;
import com.greenline.push.services.dto.QueueDTO;
import com.greenline.push.services.dto.ResponseCode;
import com.greenline.push.services.dto.ResponseDTO;

public class CheckServiceStatusServiceImpl implements CheckServiceStatusService {
    private final Logger logger = Constants.LOG_DUBBO;
    @Resource
    private CounterManager countManager;
    @Resource
    private SendStatisticsDataSchedule schedule;

    @Override
    public ResponseDTO<List<QueueDTO>> checkAllQueueStatus() {
        logger.info("客户端调用查询队列数据请求------");
        ResponseDTO<List<QueueDTO>> rResponse = new ResponseDTO<List<QueueDTO>>();
        ConcurrentHashMap<String, ProcessQueueList> tmp = countManager.getQueueMap();
        List<QueueDTO> queueList = new ArrayList<QueueDTO>();
        for (Map.Entry<String, ProcessQueueList> entry : tmp.entrySet()) {
            QueueDTO tmpdto = new QueueDTO();
            tmpdto.setQueueName(entry.getKey());
            ProcessQueueList s = entry.getValue();
            List<PriorityBlockingQueue<Packet>> b = s.getProcessQueueList();
            Integer total = 0;
            for (int i = 0; i < b.size(); i++) {
                total += b.get(i).size();
            }
            tmpdto.setCount(total);
            queueList.add(tmpdto);
        }

        rResponse.setDataResult(queueList);
        rResponse.setCode(ResponseCode.SUCCESS);
        return rResponse;
    }

    @Override
    public ResponseDTO<Boolean> resetAllQueueStatus() {
        logger.info("客户端发送清理队列计数请求------");
        ResponseDTO<Boolean> rResponse = new ResponseDTO<Boolean>();
        rResponse.setDataResult(true);
        rResponse.setCode(ResponseCode.SUCCESS);
        return rResponse;
    }

    @Override
    public ResponseDTO<Boolean> stopAllStatistics() {
        ResponseDTO<Boolean> rResponse = new ResponseDTO<Boolean>();
        logger.info("客户端发送停止统计任务请求------" + Constants.APP_KEY);
        boolean result = schedule.stopTask();
        rResponse.setDataResult(result);
        if (result) {
            rResponse.setCode(ResponseCode.SUCCESS);
        } else {
            rResponse.setCode(ResponseCode.ERROR);
        }

        return rResponse;
    }

    @Override
    public ResponseDTO<Boolean> startAllStatistics() {
        ResponseDTO<Boolean> rResponse = new ResponseDTO<Boolean>();
        logger.info("客户端发送启动统计任务请求------" + Constants.APP_KEY);
        try {
            schedule.startTask();
            rResponse.setDataResult(true);
            rResponse.setCode(ResponseCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            rResponse.setDataResult(false);
            rResponse.setCode(ResponseCode.ERROR);
        }
        return rResponse;
    }

}
